﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using TrainingWorkshop.IDAL;
using System.Data.SqlClient;

namespace TrainingWorkshop.DAL_EF
{
  public   class CourseDetailRepository_EF:ICourseDetailRepository
    {
        public void Insert(int courseId,Model.Student s)
        {
            
            using(var db=new projectbEntities())
            {
                lock (this)
                {

                    CourseDetail cd = new CourseDetail();

                    cd.CourseId = courseId;
                    cd.StudentId = s.StudentId;
                    db.AddToCourseDetails(cd);
                    db.SaveChanges(); 
                }

            }
        }

        public void Delete(int courseId,Model.Student s)
        {
            using (var db = new projectbEntities())
            {
                lock (this)
                {
                    var courseDetail = from cd in db.CourseDetails
                                       where cd.StudentId == s.StudentId
                                           && cd.CourseId == courseId
                                       select cd;
                    if (courseDetail.Count() != 0)
                    {
                        db.CourseDetails.DeleteObject(courseDetail.First());
                        db.SaveChanges();
                    }
                }
                
            }
          
        }

    }
}
